package com.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.nio.charset.StandardCharsets;

public class FileOutputStreamDecoder extends ChannelInboundHandlerAdapter {

    private int count;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(ctx.name()+"通道激活");
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(ctx.name()+"通道关闭");
        super.channelInactive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        ByteBuf buf =(ByteBuf) msg;


        int readableBytes = buf.readableBytes();
        byte[] bytes = new byte[readableBytes];


        buf.readBytes(bytes,0, readableBytes);
        String content = new String(bytes, StandardCharsets.UTF_8);
        System.out.println(content);

        ctx.writeAndFlush(count++);

        buf.release();
    }
}
